package cn.cymym.control;


import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.cymym.comm.BeingRadom;
import cn.cymym.domain.Arist;
import cn.cymym.service.ArtistService;


@WebServlet(urlPatterns= {"/addArtistInfo.do"})
public class ArtistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(ArtistServlet.class);
	private BeingRadom bRadom = new BeingRadom();
	private ArtistService aService = new ArtistService();
	
	
    public ArtistServlet() {
        super();
       
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletPath = request.getServletPath();
		String methodName = servletPath.substring(1);
		logger.debug(servletPath);
		methodName = methodName.substring(0, methodName.length()-3);
		logger.debug(methodName);
		try {
			Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			method.invoke(this, request,response);
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("方法没有找到");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		} 
	}
	protected void addArtistInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//{"Aname":name,"tel":tel,"email":email,"call":call,"address":address,
		//"graduation":graduation,"note":note, "pType":typeP},
		String name = request.getParameter("Aname");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String call = request.getParameter("call");
		String address = request.getParameter("address");
		String graduation = request.getParameter("graduation");
		String note = request.getParameter("note");
		String pType = request.getParameter("pType");
		
		HttpSession session = request.getSession();
		String ANM = (String)session.getAttribute("ANM");
		if (ANM != null) {
			Arist arist2 = new Arist(ANM, name, tel, email, call, address, graduation, pType, note);
			aService.addArtistInfo(arist2);
			response.getWriter().write("{\"msg\":1}");
		}else {
			response.getWriter().write("{\"msg\":0}");
		}
       
      
        
        response.setContentType("applicaton/json");
   //    PrintWriter out=response.getWriter();
//        String json=JSONObject.toJSONString(arist2);
//        out.print(json);
//        OutputStream outputStream=response.getOutputStream();
//        String json=JSONObject.toJSONString(arist2);
//        outputStream.write(json.getBytes());
//        outputStream.flush();
        logger.debug("已经返回");
	}
	
	
}
